#pragma once

#include "CoreMinimal.h"
#include "Components/ActorComponent.h"
#include "DamageComponent.generated.h"

DECLARE_MULTICAST_DELEGATE(FOnDeath);

UCLASS(ClassGroup=(Custom), meta=(BlueprintSpawnableComponent))
class UDamageComponent : public UActorComponent
{
	GENERATED_BODY()

public:
	UDamageComponent();

	virtual void BeginPlay() override;

	virtual void TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction) override;

	UFUNCTION(BlueprintCallable)
	void Attack(float DamageValue = 10);

	void AddHealth(const float Value) { Health = FMath::Clamp(Health + Value, 0, MaxHealth); }

	void TakeDamage(const float Value);

	float GetHealth() const { return Health; }

	template <typename UserClass>
	void AddOnDeath(UserClass* InObject, void (UserClass::*InFunc)())
	{
		OnDeath . AddUObject(InObject, InFunc);
	}

	bool IsDeath() const { return Health <= 0; }

protected:
	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "通用设置", DisplayName = "最大生命值")
	float MaxHealth = 100;

	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "通用设置", DisplayName = "当前生命值")
	float Health = 100;

	FOnDeath OnDeath;
};
